package com.yixin.hospital.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.bo.PageInfo;
import com.yixin.hospital.entity.domain.Message;
import com.yixin.hospital.entity.vo.MessageVO;
import com.yixin.hospital.entity.vo.UserMessageTypeVO;
import com.yixin.hospital.enums.MessageStatusEnum;
import com.yixin.hospital.enums.MessageTypeEnum;
import com.yixin.hospital.service.MessageService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/message")
@Api(value = "通知管理", tags = "通知管理")
@Slf4j
public class MessageController {

    @Resource
    MessageService messageService;

    @GetMapping("/user-message-type")
    @ApiOperation(value = "消息类型列表")
    public ApiResult<?> userMessageType(HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        List<Message> list = messageService.list(new LambdaQueryWrapper<Message>().select(Message::getMessageType).eq(Message::getUserId, userId).groupBy(Message::getMessageType));
        List<UserMessageTypeVO> userMessageTypeVOList = new ArrayList<>();
        for (Message message : list) {
            UserMessageTypeVO userMessageTypeVO = new UserMessageTypeVO();
            userMessageTypeVO.setMessageType(message.getMessageType());
            userMessageTypeVO.setDesc(MessageTypeEnum.getDesc(message.getMessageType()));
            userMessageTypeVOList.add(userMessageTypeVO);
        }
        return ApiResult.success(userMessageTypeVOList);
    }

    @GetMapping("/user-message-by-type")
    @ApiOperation(value = "消息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "messageType", value = "消息类型id", paramType = "query"),
    })
    public ApiResult<?> userMessageByType(@RequestParam int currentPage, @RequestParam int pageSize,
                                          @RequestParam(required = false) String messageType, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<Message>().eq(Message::getUserId, userId);
        if (Strings.isNotBlank(messageType)) wrapper.eq(Message::getMessageType, messageType);
        Page<Message> page = messageService.page(Page.of(currentPage, pageSize), wrapper);
        PageInfo<MessageVO> pageInfo = new PageInfo<>();
        BeanUtils.copyProperties(page, pageInfo);
        pageInfo.setRecords(BeanUtil.copyToList(page.getRecords(), MessageVO.class));
        for (MessageVO record : pageInfo.getRecords()) {
            record.setMessageTypeName(MessageTypeEnum.getDesc(record.getMessageType()));
            record.setStatusName(MessageStatusEnum.getDesc(record.getStatus()));
        }
        return ApiResult.success(pageInfo);
    }

    @PostMapping("/message-read")
    @ApiOperation(value = "消息已读")
    public ApiResult<?> messageRead(Long id, HttpServletRequest request) {
        String name = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        Message message = new Message();
        message.setId(id);
        message.setStatus(1);
        message.setUpdatedBy(name);
        messageService.updateById(message);
        return ApiResult.success();
    }

    @PostMapping("/message-all-read")
    @ApiOperation(value = "消息全部已读")
    public ApiResult<?> messageAllRead(HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getUserId, userId);
        List<Message> list = messageService.list(queryWrapper);
        for (Message message : list) {
            message.setStatus(1);
        }
        messageService.updateBatchById(list);
        return ApiResult.success();
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除消息")
    public ApiResult remove(@PathVariable int id) {
        messageService.removeById(id);
        return ApiResult.success();
    }

}
